package ch.bfh.abch.rems;

import ch.bfh.abch.rems.receiver.tinkerforge.TFMulticastReceiver;
import ch.bfh.abch.rems.sender.tinkerforge.TFMulticastSender;
import ch.bfh.abcm.rems.interfaces.REMSMessageReceiver;

public class REMSMulticastController {

    public static final int DEFAULT_BYTE_ARRAY_LENGTH = 512;
    private static final int DEFAULT_PORT = 9023;
    private static final String DEFAULT_MULTICAST_ADRESS = "224.0.0.251";

    private TFMulticastSender multicastSender;
    private TFMulticastReceiver multicastReceiver;
    private int port;
    private String multicastAdress;

    public REMSMulticastController() {
        this(DEFAULT_MULTICAST_ADRESS, DEFAULT_PORT);
    }

    public REMSMulticastController(String multicastAdress, int port) {
        this.multicastAdress = multicastAdress;
        this.port = port;
    }

    public TFMulticastSender getMulticastSender() {
        if (this.multicastSender == null) {
            multicastSender = new TFMulticastSender(multicastAdress, port);
        }
        return multicastSender;
    }

    public void startMulticastReceiver(REMSMessageReceiver receiverImplementation) {
        if (this.multicastReceiver == null) {
            multicastReceiver = new TFMulticastReceiver(receiverImplementation, multicastAdress, port);
        }
        multicastReceiver.startReceiving();
    }

}
